import { HttpAdapterHost, NestFactory } from '@nestjs/core'
import { AppModule } from './app.module'
// import { Logger } from '@nestjs/common'

import * as winston from 'winston'
import { utilities, WinstonModule } from 'nest-winston'
import 'winston-daily-rotate-file'
import { HttpExceptionFilter } from './filters/http-exception.filter'

async function bootstrap() {
  // const logger = new Logger()

  // 1. 创建一个winston日志实例
  const instance = winston.createLogger({
    transports: [
      new winston.transports.Console({
        level: 'info',
        format: winston.format.combine(
          winston.format.timestamp(),
          utilities.format.nestLike(),
        ),
      }),
      new winston.transports.DailyRotateFile({
        level: 'warn',
        dirname: 'logs',
        filename: 'application-%DATE%.log',
        datePattern: 'YYYY-MM-DD-HH',
        zippedArchive: true,
        maxSize: '20m',
        maxFiles: '14d',
        format: winston.format.combine(
          winston.format.timestamp(),
          utilities.format.nestLike(),
          winston.format.simple(),
        ),
      }),
    ],
  })

  // 2. 使用winston日志实例
  const logger = WinstonModule.createLogger({ instance })

  const app = await NestFactory.create(AppModule, {
    //logger: false,  ['error', 'warn'] 默认是启用的，也可以选择需要打印日志的类型
    logger,
  })

  // const httpAdapter = app.getHttpAdapter()
  const httpAdapterHost = app.get(HttpAdapterHost)
  logger.log(httpAdapterHost)

  app.useGlobalFilters(new HttpExceptionFilter(logger))

  await app.listen(process.env.PORT ?? 3000)
}
bootstrap()
